package org.example.restful.service.Impl;

import ch.qos.logback.core.util.StringUtil;
import org.example.restful.bean.Employee;
import org.example.restful.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.example.restful.service.EmployeeService;

import java.util.List;

@Service
public class EmployeeServiceImpl implements org.example.restful.service.EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;


    @Override
    public Employee getEmp(Long id) {
        Employee employee = employeeDao.getEmployeeById(id);
        return employee;
    }

    @Override
    public void saveEmp(Employee employee) {
        employeeDao.addEmp(employee);

    }


    @Override
    public void updateEmp(Employee employee) {
        //防空处理：controll可能默写属性值没填，那就需要保存修改的数值
        Long id = employee.getId();
        if (id == null) {
            return;
        }
        Employee employee1 = employeeDao.getEmployeeById(id);

        //校验数据的完整性，进行修改
        if(StringUtils.hasText(employee.getName())){employee1.setName(employee.getName());}
        if(employee.getAge() != null){employee1.setAge(employee.getAge());}
        if(StringUtils.hasText(employee.getEmail())){employee1.setEmail(employee.getEmail());}
        if(StringUtils.hasText(employee.getGender())){employee1.setGender(employee.getGender());}
        if(StringUtils.hasText(employee.getAddress())){employee1.setAddress(employee.getAddress());}
        if(employee.getSalary() != null){employee1.setSalary(employee.getSalary());}

        employeeDao.updateEmp(employee1);

    }

    @Override
    public void deleteEmp(Long id) {
        employeeDao.deleteById(id);

    }

    @Override
    public List<Employee> getEmployees() {
        List<Employee> employees = employeeDao.getEmployees();
        return employees;
    }
}
